//
//  TransferViewModel.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/6/19.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class TransferViewModel: BaseViewModel {
    
    var selectIndex:Int = 0 //当前资产选择的下标
    var model:AssetModel = AssetModel() //法币/杠杆资产/
//    var coinModel:AssetModel = AssetModel()
    var type:CoinAccountType?
//    var layOutBool:Bool = false // 划出 false 划入 true
    var sendCoin:CoinAccountType = .legalType //发送币种
    var receiveCoin:CoinAccountType = .coinType//接收币种
    
    var number:String = "0" //币种数量
    var passWord:String = ""
    
    lazy var accountRequest:AccountTransferRequest = {
        let api = AccountTransferRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    
    /// 获取model
    func archiveDefautAsset()  {
        if sendCoin == .legalType {
            if APPAssetInfo.default.legalAssetArr.count > selectIndex {
                model = APPAssetInfo.default.legalAssetArr[selectIndex]
              let _ =  self.archiveLegalAsset()
            }
        }else if sendCoin == .coinType{
            if APPAssetInfo.default.coinAssetArr.count > selectIndex {
                model = APPAssetInfo.default.coinAssetArr[selectIndex]
               let _ = self.archiveCoinAsset()
            }
        }else {
            if APPAssetInfo.default.leverAssetArr.count > selectIndex {
                model = APPAssetInfo.default.leverAssetArr[selectIndex]
                let _ = self.archiveLeverAsset()
            }
            
        }
    }
    
    ///根据所选币种获取法币/杠杆资产
    func archiveLegalAsset() -> Int {
        for  (index,model) in APPAssetInfo.default.legalAssetArr.enumerated(){
            if model.coinCode == self.model.coinCode {
                self.model = model
                return index
            }
        }
        self.model = AssetModel()
        return 0
    }
    
    ///根据所选币种 获取币币资产
    func archiveCoinAsset()  -> Int{
        for (index,model) in APPAssetInfo.default.coinAssetArr.enumerated() {
            if model.coinCode == self.model.coinCode {
               self.model = model
                return index
            }
        }
        self.model = AssetModel()
        return 0
    }
    
    ///根据所选币种 杠杆资产
    func archiveLeverAsset()  -> Int{
        for (index,model) in APPAssetInfo.default.leverAssetArr.enumerated() {
            if model.coinCode == self.model.coinCode {
                self.model = model
                return index
            }
        }
        self.model = AssetModel()
        return 0
    }
    
    
    /// 划转
    func sendAsset()  {
        if Double(number)! > self.model.currentAmount {
            CYMBShow.showMessage(kText("当前币种余额不足"))
            return
        }
       accountRequest.sendCoin = sendCoin
       accountRequest.receiveCoin = receiveCoin
        self.request(request: accountRequest, success: {[weak self] (result) in
            CYMBShow.showMessage(kText("划转成功"))
            NotificationCenter.default.post(name: NSNotification.Name.Exchange.assetDidChange, object: nil)
            for popView in ((self?.controller?.navigationController as! WrapNavigationController).wrapViewController?.navigationController?.viewControllers)! {
                let viewController = (popView as? WrapViewController)?.contentViewController
                if (viewController?.isKind(of: AssetViewController.self))! {
                    self?.controller?.navigationController?.popToViewController(viewController!, animated: true)
                    return
                }
            }
            self?.controller?.navigationController?.popToRootViewController(animated: true)
        }) { (fail) in
            if fail.errorMsg.count != 0 {
            CYMBShow.showMessage(fail.errorMsg)
            }
        }
    }
    
    
    /// 判断当前选择币种是否在法币账户中存在
    ///
    /// - Returns: bool
    func archiveIsHaveInLegal() -> Bool {
        for model in APPAssetInfo.default.legalAssetArr {
            if model.coinCode == self.model.coinCode {
                return true
            }
        }
        return false
    }
    
    
    func clear() {
        
    }
}

extension TransferViewModel: RequestCallBack,RequestDataReformer,RequestParamSource {
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        if successBlock != nil {
            successBlock!(data)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if failBlock != nil {
            failBlock!(request)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        pramas.updateValue(number, forKey: "amount")
        pramas.updateValue(model.coinCode , forKey: "coinCode")
        pramas.updateValue(passWord, forKey: "tradePwd")
        return pramas
    }
    
    
    
}
